/*
 * Copyright (C) 2011 Dirk Weißenborn, dirk.weissenborn@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.weissenborn.colabtex.client.view;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.SimpleLayoutPanel;

// TODO: Auto-generated Javadoc
/**
 * The Class TexViewImpl.
 */
public class TexViewImpl extends Composite implements TexView {

	/**
	 * The Interface Binder.
	 */
	interface Binder extends UiBinder<Widget, TexViewImpl> {
	}

	/** The Constant binder. */
	private static final Binder binder = GWT.create(Binder.class);
	
	/** The ether pad frame. */
	@UiField Frame etherPadFrame;
	
	/** The pdf frame. */
	Frame pdfFrame;
	
	/** The resource view. */
	ResourceViewComposite resourceView;

	/** The show pdf btn. */
	@UiField Button showPdfBtn;
	
	/** The show resources btn. */
	@UiField Button showResourcesBtn;
	
	/** The show log btn. */
	@UiField Button showLogBtn;
	
	/** The compile btn. */
	@UiField Button compileBtn;
	
	/** The sen tex file btn. */
	@UiField Button senTexFileBtn;
	
	/** The send pdf file btn. */
	@UiField Button sendPdfFileBtn;
	
	
	/** The right panel. */
	@UiField SimplePanel rightPanel;
	
	

	/** The listener. */
	private Presenter listener;
	
	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.TexView#setUrl(java.lang.String)
	 */
	public void setUrl(String url) {
		etherPadFrame.setUrl(url);		
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.TexView#setName(java.lang.String)
	 */
	public void setName(String name) {
		etherPadFrame.setTitle(name);
	}

	/**
	 * Instantiates a new tex view impl.
	 */
	public TexViewImpl() {
		initWidget(binder.createAndBindUi(this));
		
		pdfFrame = new Frame();
		pdfFrame.setSize("100%", "100%");		
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.TexView#setPresenter(com.weissenborn.colabtex.client.view.TexView.Presenter)
	 */
	public void setPresenter(Presenter listener) {
		this.listener = listener;
		
		resourceView = new ResourceViewComposite(listener);
		resourceView.setSize("100%", "100%");
	}
	
	/**
	 * Gets the reload btn.
	 *
	 * @return the reload btn
	 */
	public Button getReloadBtn() {
		return showPdfBtn;
	}
	
	/**
	 * Gets the pdf frame.
	 *
	 * @return the pdf frame
	 */
	public Frame getPdfFrame() {
		return pdfFrame;
	}
	
	/**
	 * Gets the ether pad frame.
	 *
	 * @return the ether pad frame
	 */
	public Frame getEtherPadFrame() {
		return etherPadFrame;
	}
	
	/**
	 * On show pdf btn click.
	 *
	 * @param event the event
	 */
	@UiHandler("showPdfBtn")
	void onShowPdfBtnClick(ClickEvent event) {
		pdfFrame.setUrl(GWT.getModuleBaseURL() + "DownloadPDF");
		rightPanel.clear();
		rightPanel.add(pdfFrame);
	}
	
	/**
	 * On show log btn click.
	 *
	 * @param event the event
	 */
	@UiHandler("showLogBtn")
	void onShowLogBtnClick(ClickEvent event) {
		TextArea text = new TextArea();
		text.setSize("100%", "100%");
		
		listener.getLog(text);
		
		rightPanel.clear();
		rightPanel.add(text);
	}
	
	/**
	 * On show resources btn click.
	 *
	 * @param event the event
	 */
	@UiHandler("showResourcesBtn")
	void onShowResourcesBtnClick(ClickEvent event) {
		rightPanel.clear();
		resourceView.getResourceBox().clear();
		listener.getResources();
		
		rightPanel.add(resourceView);
	}
	
	/**
	 * On compile btn click.
	 *
	 * @param event the event
	 */
	@UiHandler("compileBtn")
	void onCompileBtnClick(ClickEvent event) {
		listener.compilePdf();
	}
	
	/**
	 * On sen tex file btn click.
	 *
	 * @param event the event
	 */
	@UiHandler("senTexFileBtn")
	void onSenTexFileBtnClick(ClickEvent event) {
		listener.sendTexFile();
	}
	
	/**
	 * On send pdf file btn click.
	 *
	 * @param event the event
	 */
	@UiHandler("sendPdfFileBtn")
	void onSendPdfFileBtnClick(ClickEvent event) {
		listener.sendPdfFile();
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.TexView#deleteResource(java.lang.String)
	 */
	public void deleteResource(String filename) {
		for (int i = 0; i < resourceView.getResourceBox().getItemCount(); i++) {
			if(resourceView.getResourceBox().getItemText(i).equals(filename)) {
				resourceView.getResourceBox().removeItem(i);
				break;
			}
		}
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.TexView#setResources(java.util.List)
	 */
	public void setResources(List<String> result) {
		for (String string : result) {
			resourceView.getResourceBox().addItem(string);
		}				
	}
}
